package Redis.MultilDb.conf;

import java.time.Duration;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;

@Configuration
public class RedisConfig {
	
	
	@Bean
	@ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
	@Scope(value = "prototype")
	public GenericObjectPoolConfig redisPool(){
	    return new GenericObjectPoolConfig();
	}
	
	@Bean
	@ConfigurationProperties(prefix = "spring.redis.redis-onedb")
	public RedisStandaloneConfiguration redisConfigA(){
	    return new RedisStandaloneConfiguration();
	}
	
	
	@Primary
	@Bean
	public LettuceConnectionFactory factoryA(GenericObjectPoolConfig config, RedisStandaloneConfiguration redisConfigA){
	    LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
	            .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
	    return new LettuceConnectionFactory(redisConfigA, clientConfiguration);
	}
	
	
	@Bean(name = "oneRedis")
	public StringRedisTemplate redisBusTemplate(@Qualifier("factoryA") LettuceConnectionFactory factoryA){
	    StringRedisTemplate template = getRedisTemplate();
	    template.setConnectionFactory(factoryA);
	    return template;
	}
	
	
	@Bean
	@ConfigurationProperties(prefix = "spring.redis.redis-twodb")
	public RedisStandaloneConfiguration redisConfigB(){
	    return new RedisStandaloneConfiguration();
	}
	
	@Bean
	public LettuceConnectionFactory factoryB(GenericObjectPoolConfig config, RedisStandaloneConfiguration redisConfigB){
	    LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
	            .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
	    return new LettuceConnectionFactory(redisConfigB, clientConfiguration);
	}
	
	
	@Bean(name = "twoRedis")
	public StringRedisTemplate redisLoginTemplate(@Qualifier("factoryB")LettuceConnectionFactory factoryB){
	    StringRedisTemplate template = getRedisTemplate();
	    template.setConnectionFactory(factoryB);
	    return template;
	}
	
	private StringRedisTemplate getRedisTemplate(){
	    StringRedisTemplate template = new StringRedisTemplate();
	    template.setValueSerializer(new GenericFastJsonRedisSerializer());
	    template.setValueSerializer(new StringRedisSerializer());
	    return template;
	}
}